<?php

/**
 * ECSHOP WAP首页
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: testyang $
 * $Id: goods_list.php 15013 2008-10-23 09:31:42Z testyang $
*/

define('IN_ECS', true);
define('ECS_ADMIN', true);

require(dirname(__FILE__) . '/includes/init.php');

$act = isset($_GET['act']) ? $_GET['act'] : 'default';

/*此处为分页中每一页显示的数量，每一页显示一个*/
$page_size = 1;

/*活动列表查询*/
if($act == 'default'){
// die("00");
	/*查询条件*/
	$keywords = isset($_REQUEST['keywords']) ? trim($_REQUEST['keywords']) : '';//搜索关键词
	$cat_id   = isset($_REQUEST['cat_id']) ? intval($_REQUEST['cat_id']) : '';//活动分类id
	$area     = isset($_REQUEST['area']) ? intval($_REQUEST['area']) : '';//活动所属区域
	$states   = isset($_REQUEST['states']) ? intval($_REQUEST['states']) : '';//活动状态

	/*查询数据*/
	$act_list = get_act_list($keywords,$cat_id,$area,$states,$page_size);

	$smarty->assign('category', $act_list['list']);
	$smarty->assign('page_count', $act_list['page_count']);
	
	$smarty->assign('keywords', $keywords);
	$smarty->assign('cat_id',   $cat_id);
	$smarty->assign('area', $area);
	$smarty->assign('states', $states);
		
	/* y-分类列表 */
	$sql = "SELECT cat_name,cat_id FROM " . $GLOBALS['ecs']->table('category').'where is_open= 1';
	$result = $db->getAll($sql);
	$smarty->assign('valuea',$result);
	
	/* 区域查询 */
	$sql= 'SELECT * FROM ' .$GLOBALS['ecs']->table('area'). 'where is_open= 1' ;
	$result = $GLOBALS['db']->getALL($sql);
	$smarty->assign('quyu',$result);
	if($cat_id){
		$sql="select cat_name,cat_id from {$ecs->table('category')} where cat_id='$cat_id'";
	
		$cat = $db->getRow($sql);
		$smarty->assign('cat',$cat);
	}
	// $cat_id = intval($_GET['cat_id']);
	
	$smarty->display('goods_list.dwt');

}
/*筛选加载数据*/
else if($act == "search"){
	
	/*此处为本页面通过搜索框搜索，搜索的过程及结果在此处执行，通过ajax传到页面上*/
	$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';//搜索关键词
	$cat_id   = isset($_POST['cat_id']) ? intval($_POST['cat_id']) : '';//活动分类id
	$area     = isset($_POST['area']) ? intval($_POST['area']) : '';//活动所属区域
	$states   = isset($_POST['states']) ? intval($_POST['states']) : '';//活动状态
	
	/*查询数据*/
	$act_list = get_act_list($keywords,$cat_id,$area,$states,$page_size);
	
	$smarty->assign('category', $act_list['list']);
	$smarty->assign('page_count', $act_list['page_count']);	
	
	if(empty($act_list['list'])){
		
		$ret['error'] = 1;
		
	}else{
		
		$ret['error'] = 0;
		
	}
	
	$ret['page_count'] = $act_list['page_count'];
	$ret['content'] = $smarty->fetch('library/goods_list.lbi');
	
	die(json_encode($ret));

}
/*ajax分页加载数据*/
else if($act == "ajax_query"){

	$ret = array();
    $page = $_POST['page'];
	
	/*查询条件*/
	$keywords = isset($_POST['keywords']) ? trim($_POST['keywords']) : '';//搜索关键词
	$cat_id   = isset($_POST['cat_id']) ? intval($_POST['cat_id']) : '';//活动分类id
	$area     = isset($_POST['area']) ? intval($_POST['area']) : '';//活动所属区域
	$states   = isset($_POST['states']) ? intval($_POST['states']) : '';//活动状态
	
	$where = " WHERE 1 ";
	/*活动关键字*/
	if(!empty($keywords)){
		
		$where .= " AND ac.activity_name LIKE '%" . $keywords . "%'";
		
	}
	
	/*活动区域*/
	if(!empty($area)){
		
		$where .= " AND ar.area_id = '$area'";	
		
	}
	
	/*活动分类*/
	if(!empty($cat_id)){	
	
		$where .= " AND ca.cat_id = '$cat_id'";	
		
	}
	
	/*活动状态转换*/
	if($states > 0)
	{
		/*当前时间获取*/
		$showtime = date("Y-m-d H:i");
		$NowTime  = !empty($showtime) ? local_strtotime($showtime) : 0;
		
		switch ($states) {
			case '1':
				$where .= " AND ac.startsign_time > $NowTime ";
				break;
			case '2':
				$where .= " AND ac.endsign_time > $NowTime AND ac.startsign_time < $NowTime ";
				break; 
			case '3':
				$where .= " AND ac.activity_stime > $NowTime AND ac.endsign_time < $NowTime ";
				break; 
			case '4':
				$where .= " AND ac.activity_etime > $NowTime AND ac.activity_stime < $NowTime ";
				break; 
			case '5':
				$where .= " AND ac.activity_etime < $NowTime ";
				break;
			default:
				$where .= " AND  1 ";
				break;
		}

	}
	

	/*查询数据*/
	$result = array();
	$sql='SELECT * FROM ' . $GLOBALS['ecs']->table('activity'). ' AS ac ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS ca ON ca.cat_id = ac.cat_id ' . ' LEFT JOIN ' . $GLOBALS['ecs']->table('area') . ' AS ar ON ar.area_id = ac.area_id ' . $where .' ORDER BY ac.endsign_time DESC limit ' . ($page+1)*$page_size .",".$page_size;

    $result = $GLOBALS['db']->getAll($sql);

	if(!empty($result)){

		foreach($result as $k=>$value){
			
			/*活动状态转换*/
			$result[$k]['activity_status'] = get_activity_status($value);
			$result[$k]['activity_img'] = "../admin/".$value['activity_img'];
			/*时间格式化*/
			$result[$k]['activity_stime'] = local_date('Y-m-d',$value['activity_stime']);
			$result[$k]['activity_etime'] = local_date('Y-m-d',$value['activity_etime']);
			$result[$k]['endsign_time'] = local_date('Y-m-d H:i', $value['endsign_time']);

		} 
	}
	
	/*ajax返回值*/
	$smarty->assign('result', $result);
	
	$ret['page'] = $page+1;
	
	$html = "";
	
	foreach($result as $key=>$value){
		
		$html .="<li class='mui-table-view-cell mui-media'><a href='goods.php?activity_id=".$value['activity_id']."'><img class='mui-media-object mui-pull-left' src='".$value['activity_img']."'><div class='mui-media-body'><h3 class='mui-ellipsis-2' >".$value['activity_name']."</h3><div class='adds' ><i class='icons-all icon_adds'></i>地点：<span>".$value['activity_place']."</span></div><div class='adds' ><i class='icons-all icon_date'></i>".$value['activity_stime']."至".$value['activity_stime']."</div><div class='tfont'>报名结束时间：".$value['endsign_time']."</div><div class='tfont'><i class='icons-all icon_eye'></i><span id='num'>(".$value['number'].")</span>";
		
		
		if($value['activity_status'] == '1'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#ff5842'>预热中</span>";
			
		}elseif($value['activity_status'] == '2'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#98cb15'>报名中</span>";
			
		}elseif($value['activity_status'] == '3'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#109646'>活动中</span>";

			
		}elseif($value['activity_status'] == '4'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#0CD0E4'>报名结束</span>";
			
		}elseif($value['activity_status'] == '5'){
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#b3b3b3'>活动结束</span>";
			
		}else{
			
			$html .="<span class='s-btn s-btn-green mui-pull-right'style='background:#FF7600'>未设置</span>";
			
		}
			
		$html .="</div></div></a></li>";
		
	}
	
	$ret['info'] = $html;

    die(json_encode($ret));
	
}


/**
* 根据条件查询数据列表
* $keywords  trim  关键词
* $cat_id    intval 活动所属分类
* $area      intval 活动所属区域
* $states    intval 活动状态 0不限 1预热中 2报名中 3报名结束 4活动中 5活动结束
* $pagesize  intval 分页数
*/

function get_act_list($keywords='',$cat_id = '',$area ='',$states='',$pagesize = 6){
		
	/*组合查询条件语句*/
	$where = " WHERE 1 ";
	
	/*活动关键字*/
	if(!empty($keywords)){
		
		$where .= " AND ac.activity_name LIKE '%" . $keywords . "%'";
		
	}
	
	/*活动区域*/
	if(!empty($area)){
		
		$where .= " AND ar.area_id = '$area'";	
		
	}
	
	/*活动分类*/
	if(!empty($cat_id)){	
	
		$where .= " AND ca.cat_id = '$cat_id'";	
		
	}
	
	/*活动状态转换*/
	if($states > 0)
	{
		/*当前时间获取*/
		$showtime = date("Y-m-d H:i");
		$NowTime  = !empty($showtime) ? local_strtotime($showtime) : 0;
		
		switch ($states) {
			case '1':
				$where .= " AND ac.startsign_time > $NowTime ";
				break;
			case '2':
				$where .= " AND ac.endsign_time > $NowTime AND ac.startsign_time < $NowTime ";
				break; 
			case '3':
				$where .= " AND ac.activity_stime > $NowTime AND ac.endsign_time < $NowTime ";
				break; 
			case '4':
				$where .= " AND ac.activity_etime > $NowTime AND ac.activity_stime < $NowTime ";
				break; 
			case '5':
				$where .= " AND ac.activity_etime < $NowTime ";
				break;
			default:
				$where .= " AND  1 ";
				break;
		}

	}
	
	/*分页*/
	$sql='SELECT count(*) FROM ' . $GLOBALS['ecs']->table('activity'). ' AS ac ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS ca ON ca.cat_id = ac.cat_id ' . ' LEFT JOIN ' . $GLOBALS['ecs']->table('area') . ' AS ar ON ar.area_id = ac.area_id ' . $where ;
	
	$record_count = $GLOBALS['db']->getOne($sql);
	
	/*分页数*/
	$page_count = $record_count > 0 ? ceil($record_count / $pagesize) : 1;
	
	
	/*查询数据*/
	$result = array();
	$sql='SELECT * FROM ' . $GLOBALS['ecs']->table('activity'). ' AS ac ' . 'LEFT JOIN ' . $GLOBALS['ecs']->table('category') . ' AS ca ON ca.cat_id = ac.cat_id ' . ' LEFT JOIN ' . $GLOBALS['ecs']->table('area') . ' AS ar ON ar.area_id = ac.area_id ' . $where .' ORDER BY ac.endsign_time DESC limit 0,'.$pagesize;

    $result = $GLOBALS['db']->getAll($sql);

	if(!empty($result)){

		foreach($result as $k=>$value){
			
			/*活动状态转换*/
			$result[$k]['activity_status'] = get_activity_status($value);
			$result[$k]['activity_img'] = "../admin/".$value['activity_img'];
			/*时间格式化*/
			$result[$k]['activity_stime'] = local_date('Y-m-d',$value['activity_stime']);
			$result[$k]['activity_etime'] = local_date('Y-m-d',$value['activity_etime']);
			$result[$k]['endsign_time'] = local_date('Y-m-d H:i', $value['endsign_time']);

		} 
	}
	
	$arr = array('list' => $result, 'page_count' => $page_count, 'record_count' => $record_count);

    return $arr;
	
}



/**
* 活动状态转换
* $activity_stime  time  活动开始时间
* $activity_etime  time  活动结束时间
* $startsign_time  time  报名开始时间
* $endsign_time    time  报名结束时间
*/

function get_activity_status($act_info = array()){
	
	/*当前时间获取*/
	$showtime=date("Y-m-d H:i");
	$NowTime = !empty($showtime) ? local_strtotime($showtime) : 0;
	
	$result = '';
	
	if($NowTime < $act_info['startsign_time']){

		$result = '1';

	}elseif ($act_info['endsign_time'] > $NowTime && $NowTime > $act_info['startsign_time']) {

		$result = '2';

	}elseif ($act_info['activity_stime'] > $NowTime && $NowTime > $act_info['startsign_time']) {	

		$result = '4';

	}elseif ($act_info['activity_etime'] > $NowTime && $NowTime > $act_info['activity_stime']) {

		$result = '3';

	}elseif ($NowTime > $act_info['activity_etime']) {	

		$result = '5';

	}else{	

		$result = '0';

	}

	return $result;
	
}

?>